package pc.system;

import java.util.*;
import pc.sql.*;
import pc.util.*;
import pc.sql.value.*;

public class Tasks extends Actions
{
  //private int permission = 0;

  private boolean focused = false;
  private boolean enabled = false;
  private Button button = null;

  private final Vector taskList = new Vector();
  private String name = null;
  private String linkPage = "";

  public Tasks(ConnectionPool connPool)
  {
    super(connPool);
  }

  public Tasks(Tasks owner,String name,String btn_name)
  {
    super(owner);
    this.name = name;
    owner.taskList.addElement(this);
    if (btn_name == null) btn_name = name;
    if (this.getLanguage().equals("vn")) btn_name += "_v";
    button = new Button(btn_name);
    setFocus(false);
    setEnable(false);
    setLinkPage();
    if (new_version) {
      setPermission(readPermission());
      setEnable(getPermission() > 0);
    }
  }

  public Tasks(Tasks owner,String name)
  {
    this(owner,name,null);
  }

  public Tasks(Tasks owner)
  {
    super(owner);
  }

  public String getImage()
  {
    return "images/" + name + ".jpg";
  }

  public String getName()
  {
    return name;
  }

  public int getChildSize()
  {
    return this.taskList.size();
  }

  public Tasks getTask(int i)
  {
    if ( (i < 0) || (i > taskList.size() - 1)) return null;
    return (Tasks)taskList.get(i);
  }

  public Tasks getTask(String name)
  {
    for (int i = 0; i < taskList.size(); i++) {
      Tasks task = (Tasks) taskList.get(i);
      if (task.name.equals(name)) return task;
    }
    return null;//new Tasks(this,null);
  }

  private void setLinkPage()
  {
    if (name == null) return;
    linkPage = this.name;
    Tasks owner = (Tasks)this.getOwner();
    while (owner != null) {
      if (owner.name != null) {
        String s = owner.name.trim();
        if (s.length() > 0) linkPage = s + '.' + linkPage;
      }
      owner = (Tasks)owner.getOwner();
    }
    if (linkPage != null) {
      if (linkPage.length() > 0) linkPage = linkPage + ".jsp";
    }
  }

  public String getLinkPage()
  {
    return linkPage;
  }

  public String doLink()
  {
    String link = this.enabled ? this.linkPage : "";
    return (!focused) ? link : "";
  }

  public Button getButton()
  {
    return button;
  }


  public void setEnable(boolean value)
  {
    enabled = value;
    if (button != null) button.setEnable(value);
  }

  public void setChildEnable(boolean value)
  {
    for (int i = 0; i < taskList.size(); i++) {
      Tasks task = (Tasks)taskList.get(i);
      task.setEnable(value);
    }
  }

  public boolean getEnable()
  {
    return enabled;
  }

  public void clearFocus()
  {
    for (int i = 0; i < taskList.size(); i++) {
      Tasks task = (Tasks)taskList.get(i);
      //task.setFocus(false);
      if (task.getButton() != null) {
        task.getButton().setFocus(false);
        task.focused = false;
      }
    }
  }

  public void setFocus(boolean value)
  {
    Tasks owner = (Tasks)this.getOwner();
    if (owner != null) {
      owner.clearFocus();
      owner.setFocus(value);
    }
    if (!getEnable()) value = false;
    focused = value;
    if (button != null) button.setFocus(value);
  }

  public boolean getFocus()
  {
    return focused;
  }

  public Tasks findChildFocus()
  {
    for (int i = 0; i < taskList.size(); i++) {
      Tasks task = (Tasks)taskList.get(i);
      if (task.getFocus()) return task;
    }
    return null;
  }

  public Tasks getChildFocus()
  {
    Tasks task = findChildFocus();
    if (task == null) {
      //task = new Actions(null,null);
      if (!this.taskList.isEmpty()) {
        task = (Tasks)taskList.firstElement();
        if (task != null) task.setFocus(true);
      }
    }
    return task;
  }

  public void forwardToChild(String uid)
  {
    Tasks item = this.getChildFocus();
    System.out.println("LinkPage:-"+item.getLinkPage());
    if (item != null) {
      this.getResponse().forwardTo(uid,item.getLinkPage());
    } else {
      //System.out.println("Focused not found");
    }
  }

  public int getPermission(long user_id,String menu,String parent_menu,int level)
  {
    if (parent_menu != null && parent_menu.length() == 0) parent_menu = null;

    String sql =
        "SELECT * FROM access_list " +
        "WHERE user_id = ? " +
        "  AND menu = ? " +
        "  AND (parent_menu = ? OR ?) " +
        "  AND level = ?";
    Vector vp = new Vector();
    vp.addElement(new LongValue(user_id));
    vp.addElement(new StringValue(menu));
    vp.addElement(new StringValue(parent_menu));
    vp.addElement(new BooleanValue(parent_menu == null));
    vp.addElement(new IntValue(level));

    Record rc = new Record(this.getSQLCommand().executeQuery(sql,vp));
    int perm = rc.getIntValue("permission");
    rc.close();
    return perm;
  }

  public int readPermission()
  {
    Actions o = this;
    int level = -1;
    while (o != null) {
      o = o.getOwner();
      level++;
    }
    if (level <= 0) return 0;

    //long user_id = this.getAuth().getUserID();
    long user_id = 0;
    if (getRoot() != null) {
      user_id = ( (Auth) getRoot()).getUserID();
    }
    String menu = this.getName();
    String parent_menu = ((Tasks)this.getOwner()).getName();
    return this.getPermission(user_id,menu,parent_menu,level);
  }

  public long updateUserInfo(long id)
  {
    Table tab = new Table(this,"user_master");

    tab.addParam("user_name");
    tab.addParam("full_name");
    tab.addParam("max_logon");
    tab.addParam("company_id");
    tab.addParam("admin");

    id = tab.update(id);

    String pw = this.getRequest().getStringValue("password").trim();
    String re = this.getRequest().getStringValue("re_password").trim();

    if (pw.length() == 0) return id;

    if (!pw.equals(re)) {
      this.getSysLog().setMessage("The password was not correctly confirmed.");
      return id;
    }
    String sql =
        "UPDATE user_master SET password = PASSWORD('" + pw + "') WHERE id = " + id;
    getSQLCommand().executeUpdate(sql);
    return id;
  }

  public long doUpdate(String table_name,long id)
  {
    Table table = new Table(this,table_name);
    table.addParams();
    return table.update(id);
  }

  public String getNextRefNumber(String ref_number)
  {
    if (ref_number == null) ref_number = "00-00000";
    String s = ref_number.trim();
    if (s.length() < 8) s = "00-00000";
    int p = s.lastIndexOf(' ');

    if (p >= 0) s = s.substring(p + 1).trim();
    else s = "00-00000";

    p = s.indexOf('-');
    if (p < 0) p = 2;
    int cy = Utils.strToInt(s.substring(0,p));
    int cn = Utils.strToInt(s.substring(p+1));
    int dy = Utils.getCurYear() % 100;
    if (dy != cy) return Utils.formatNumber(dy,2) + "-" + Utils.formatNumber(1,5);
    return Utils.formatNumber(cy,2) + "-" + Utils.formatNumber(cn+1,5);
  }

  public String getNewRefNumber(String table, String prefix)
  {
    String ref = prefix + " 00-00000";
    if (table == null) return prefix +" " + getNextRefNumber(ref);
    int len = prefix.length();
    String sql =
        "SELECT MAX(ref_number) as ref_number " +
        "FROM " + table + " " +
        "WHERE SUBSTRING(ref_number,1," + len +") = '" + prefix + "'";
    Record rc = new Record(this.getSQLCommand().executeQuery(sql));
    if (!rc.isEmpty()) ref = rc.getString("ref_number");
    //String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : prefix + " 00-00000";
    rc.close();
    return prefix +" " + getNextRefNumber(ref);
  }
  
  public String getNextRefNo(String ref_number)
  {
    if (ref_number == null) ref_number = "00000";
    String s = ref_number.trim();

    int cn = Utils.strToInt(s);
    return  Utils.formatNumber(cn+1,5);
  }  
}
